package com.pagepub.core.tag.message;

import java.io.IOException;

import javax.servlet.jsp.JspException;
import javax.servlet.jsp.tagext.TagSupport;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import com.pagepub.core.utils.StringUtil;



/**
 * 处理webmsg.jsp提示组件
 * @author ggd
 *
 */
public class MessageBoxTag extends TagSupport
{
	
	private static final long serialVersionUID = 1L;
	
	
	private static Log log = LogFactory.getLog(MessageBoxTag.class);
	
	
	private String name=HtmlMessageBox.MESSAGE_PAGES_ATTRIBUTE_NAME;
	public int doStartTag() throws JspException
	{
		if(name==null||name.trim().length()==0)
		{
			log.error("没有指定request.setAttribute()保存的名称,TagSupport.doStarTag()");
			throw new JspException("没有指定request.setAttribute()保存的名称!");
		}
		HtmlMessageBox hmb=(HtmlMessageBox)this.pageContext.getRequest().getAttribute(name);
		if(hmb==null)
		{
			log.error("没有指定request.setAttribute()保存的名称,TagSupport.doStarTag()");
			throw new JspException("没有指定request.setAttribute()保存的名称!");
		}

		try
		{
			this.pageContext.getOut().print(getHtmlMessageBox2Js(hmb));
		}
		catch (IOException e)
		{
			log.error("输出信息错误,MessageBoxTag.doStartTag()",e);
			throw new JspException("输出信息错误,MessageBoxTag.doStartTag()",e);
		}
		return 1;
	}
	private String getHtmlMessageBox2Js(HtmlMessageBox hmb)
	{
		StringBuffer str=new StringBuffer("{");
		str.append("enabledTimer:"+hmb.isEnabledTimer()+",");
		str.append("timer:"+hmb.getTimer()+",");
		str.append("targetName:\""+hmb.getTargetName()+"\",");
		String tmpUrl="";
		String tmpLink="";
		
		for (int i=0;i<hmb.getForwardUrl().length;i++)
		{
			tmpUrl+="\""+hmb.getForwardUrl()[i]+"\",";
			tmpLink+="\""+hmb.getLinkMsg()[i]+"\",";
		}
		
		str.append("forwardUrl:[" + StringUtil.trimChr(tmpUrl, ",")+"],");
		str.append("linkMsg:["+StringUtil.trimChr(tmpLink, ",")+"],");
		str.append("message:\""+hmb.getMessage()+"\"}");
		return str.toString();
	}

}

